/* 
 * File:   SourceFile.cpp
 * Author: k0l0b0k
 * 
 * Created on 8 Октябрь 2009 г., 15:46
 */

#include "SourceFile.h"
#include "SourceReader.h"
#include "Namespace.h"

SourceFile::SourceFile(Project* pProject, QString pFileName)
	: mProject(pProject), mFileName(pFileName), mReader(pFileName, pProject)
{
}

SourceFile::~SourceFile()
{
}

bool SourceFile::Parse()
{
	mReader.Open();
	mReader.Modifiers = EEModifiers::None;
	while (true)
	{
		Token* token = mReader.NextToken();
		Token* nametoken = 0;
		if (!token) return true;
		else if (token->Text == Keywords::Using)
		{
			if (mReader.Modifiers != EEModifiers::None)
			{
				Output.Error(token, "Extra modifiers on using keyword. Please remove it.");
			}
			else
			{
				nametoken = mReader.NextToken();
				if (!nametoken) return false;
				if (!mReader.IsSeparator()) return false;
				mUsings.push_back(nametoken);
			}
		}
		else if (token->Text == Keywords::Namespace)
		{
			nametoken = mReader.NextToken();
			if (!nametoken) return false;
			Namespace* ns = mProject->GetOrCreateNamespace(nametoken->Text);
			ns->InheritModifiers = mReader.Modifiers;
			ns->Parse(&mReader);
		}
		else
		{
			mReader.AddModifier(token, (EModifiers)(EEModifiers::Internal | EEModifiers::Private |
									EEModifiers::Protected | EEModifiers::Public));
		}
	}
}
